package com.apes.hr.transfer.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.hr.base.model.Employee;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * 功能：工作交接单
 * @author HuJianlin
 * @create 2018-05-05 15:11
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = WorkHandover.CONST_TABLE_NAME)
public class WorkHandover  extends Model {
    public static final String CONST_TABLE_NAME = "hr_Work_Handover";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "工作交接单", prefix = "H", padding = 7)
    private String id;   //工作交接单号

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "leave_employee_id", referencedColumnName = "ID")
    private Employee leaveEmployee; //卸任员工

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "take_over_employee_id", referencedColumnName = "ID")
    private Employee takeOverEmployee; //接替员工

    @Column(name = "content", length = 500)
    private String content;  //内容

    @Column(name = "source_type", length = 16)
    private String sourceType;  //来源单据类型

    @Column(name = "source_number", length = 16)
    private String sourceNumber;  //来源单号

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "confirm_uid", referencedColumnName = "id")
    private PartyRole confirmUid; //确认人

    @Column(name = "confirm_date")
    private Date confirmDate; //确认日期

    @Column(name = "state", length = 10, nullable = false)
    private String state; //状态： ('create', '制单'), ('confirm', '确认')

    @Transient
    private PartyRole _creator;

    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }
}
